<?php
include "templates/header.php";
//print_r($_FILES);die;
if(isset($_POST['add_file'])){
    if ($_FILES['file']) {
        $blacklist = array(".php", ".phtml", ".php3", ".php4");
        foreach ($blacklist as $item) {
            if (preg_match("/$item\$/i", $_FILES['file']['name'])) {
                echo "We do not allow uploading PHP files\n";
                exit;
            }
        }
        $add_file = array();
        $add_file['title'] = $_POST['title'];
        $add_file['show'] = $_POST['show'] ? $_POST['show'] : 0;
        $uploaddir = 'files/'; // Relative path under webroot
        if(!is_dir(ROOT_PATH . $uploaddir)){
            mkdir(ROOT_PATH . $uploaddir);
        }
        $filetype = explode(".",$_FILES['file']['name']);
        $filetype = array_pop($filetype);
        $uploadfile = $uploaddir . md5($_FILES['file']['name']) . "." . $filetype;
        $add_file['path'] = $uploadfile;
        move_uploaded_file($_FILES['file']['tmp_name'], ROOT_PATH . $uploadfile);
        mysql_insert("files", $add_file);
    }
}

if(isset($_POST['delete_file'])){
    $filename=mysql_value("SELECT `path` FROM `files` WHERE `id` = ?", $_POST['delete_file']);
    mysql_delete('files', $_POST['delete_file']);
    unlink(ROOT_PATH . $filename);
}

if(isset($_POST['update_file'])){
    if($_FILES['file']['name']){
        $filename=mysql_value("SELECT `path` FROM `files` WHERE `id` = ?", $_POST['update_file']);
        unlink(ROOT_PATH . $filename);
        move_uploaded_file($_FILES['file']['tmp_name'], ROOT_PATH . $filename);
    }
    $update_fields = array();
    $update_fields['title'] = $_POST['data'][$_POST['update_file']]['title'];
    $update_fields['show'] = $_POST['data'][$_POST['update_file']]['show'] ? $_POST['data'][$_POST['update_file']]['show'] : 0;
    mysql_update('files', $update_fields, $_POST['update_file']);
}

$files = mysql_select_array("SELECT * FROM `files`");


?>
<table border="1" cellspacing="0" style="text-align: center; width: 100%;">
    <tr>
        <td>Название</td>
        <td style="width: 70px;">Показать</td>
        <td style="width: 300px;">Путь</td>
        <td style="width: 90px;">&nbsp;</td>
        <td style="width: 90px;">&nbsp;</td>
    </tr>
    <tr>
        <form action="" method="post" enctype="multipart/form-data">
            <input type="hidden" name="add_file" value="1">
            <td><input type="text" name="title" style="width: 100%;" /></td>
            <td><input type="checkbox" name="show" value="1" /></td>
            <td style="text-align: right;"><input type="file" name="file" /></td>
            <td><input type="submit" value="Добавить" style="width: 100%;"  /></td>
        </form>
            <td>&nbsp;</td>
    </tr>
    <?if($files):?>
        <?foreach($files as $file):?>
            <tr>
                <form action="" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="update_file" value="<?=$file['id'];?>" />
                    <td><input type="text" name="data[<?=$file['id'];?>][title]" value="<?=$file['title'];?>" style="width: 100%;" /></td>
                    <td><input type="checkbox" name="data[<?=$file['id'];?>][show]" value="1" <?if($file['show']):?>checked="checked"<?endif;?> /></td>
                    <td style="text-align: left; padding-left: 10px;"><a href="<?=BASE_URL . $file['path'];?>">ссылка</a>&nbsp;&nbsp;&nbsp;<input type="file" name="file" /></td>
                    <td><input type="submit" value="Обновить" style="width: 100%;"  /></td>
                </form>
                    <td>
                        <form action="" method="post" style="margin: 0;">
                            <input type="hidden" name="delete_file" value="<?=$file['id'];?>" />
                            <input type="submit" value="Удалить" style="width: 100%;" />
                        </form>
                    </td>
            </tr>
        <?endforeach;?>
    <?endif;?>
    
</table>
<?php include ROOT_PATH ."admin/templates/footer.php"; ?>